﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Hartley.ApplicationLogic;
using Hartley.DTOs;
using Hartley.Util;

namespace WebApplication1
{
    public partial class viewAthleticDataYrWise : System.Web.UI.Page
    {
        // Rows property to hold the Rows in the ViewState
        protected int Rows
        {
            get
            {
                return ViewState["Rows"] != null ? (int)ViewState["Rows"] : 0;
            }
            set
            {
                ViewState["Rows"] = value;
            }
        }

        // Columns property to hold the Columns in the ViewState
        protected int Columns
        {
            get
            {
                return ViewState["Columns"] != null ? (int)ViewState["Columns"] : 0;
            }
            set
            {
                ViewState["Columns"] = value;
            }
        }

        public List<AthleticDataDTO> AllAthleticData
        {
            get
            {
                if (Session["AllAthleticData"] == null)
                {
                    return athleticAL.GetAllAthleticData();
                }
                else
                {
                    return (List<AthleticDataDTO>)Session["AllAthleticData"];
                }
            }
            set
            {
                Session["AllAthleticData"] = value;
            }
        }

        
        AthleticAL athleticAL;
        SportsUtil sportsUtil;
        protected int currentPageNumber = 1;
        private const int PAGE_SIZE = 10;
        private const int YEAR_PER_ROW = 4;
        List<int> allAthleticYrs;
        string yrWiseQueryString;
       
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                BindData();
            }
        }

        private void CreateDynamicTable(List<int> allAthleticYrsOfOnePage)
        {
            PlaceHolder1.Controls.Clear();

            // Fetch the number of Rows and Columns for the table 
            // using the properties


            int tblRows = allAthleticYrsOfOnePage.Count / YEAR_PER_ROW;
            int tblCols = 0;


            // Create a Table and set its properties 
            Table tbl = new Table();
            tbl.Width = new Unit("100%");
            tbl.BorderStyle = BorderStyle.Solid;
            // Add the table to the placeholder control
            PlaceHolder1.Controls.Add(tbl);
            // Now iterate through the table and add your controls 
            for (int i = 0; i <= tblRows; i++)
            {
                TableRow tr = new TableRow();
                tr.Width = new Unit("25%");



                if (i < tblRows && tblRows >= 1)
                {
                    tblCols = YEAR_PER_ROW;
                }
                else
                {
                    tblCols = allAthleticYrsOfOnePage.Count % YEAR_PER_ROW;
                }


                for (int j = 0; j < tblCols; j++)
                {
                    TableCell tc = new TableCell();

                    HyperLink hlAthleticYrCell = new HyperLink();
                    hlAthleticYrCell.Text = allAthleticYrsOfOnePage[i * YEAR_PER_ROW + j].ToString();

                    if (string.IsNullOrEmpty(yrWiseQueryString))
                    {
                        hlAthleticYrCell.NavigateUrl = "viewAthleticDataAsGrid.aspx?" + allAthleticYrsOfOnePage[i * YEAR_PER_ROW + j].ToString();
                    }
                    if (i % 2 == 0)
                    {
                        tc.Style.Add("background-color", "#003399");
                        tc.ForeColor = System.Drawing.Color.White;
                    }
                    else
                    {
                        tc.Style.Add("background-color", "White");
                        tc.ForeColor = System.Drawing.Color.Blue;
                    }



                    // Add the control to the TableCell
                    tc.Controls.Add(hlAthleticYrCell);
                    // Add the TableCell to the TableRow
                    tr.Cells.Add(tc);
                }

                // Add the TableRow to the Table
                tbl.Rows.Add(tr);
            }

            // This parameter helps determine in the LoadViewState event,
            // whether to recreate the dynamic controls or not

            ViewState["dynamictable"] = true;
        }


        private void BindData()
        {

            athleticAL = new AthleticAL();
            allAthleticYrs = new List<int>();
            List<int> allAthleticYrsOfOnePage = new List<int>();

            yrWiseQueryString = Request.QueryString.ToString();

            if (string.IsNullOrEmpty(yrWiseQueryString))
            {
                allAthleticYrs = athleticAL.GetAllAthleticData().OrderByDescending(b=>b.Year).Select(a => a.Year).Distinct().ToList();

            }
           





            // get the total rows 
            double totalRows = (int)allAthleticYrs.Count;

            lblAllAthleticYrPage.Text = "Displaying all athletic Data of College year wise- Page ";
            lblAllAthleticYrOf.Text = "  of  ";
            lblAllAthleticYrTotalPage.Text = CalculateTotalPages(totalRows).ToString();

            lblAllAthleticYrCurrPage.Text = currentPageNumber.ToString();

            if (Convert.ToInt32(lblAllAthleticYrTotalPage.Text) < 3)
            {
                if (Int32.Parse(lblAllAthleticYrTotalPage.Text) <= 0)
                {
                    btnAllAthleticYrFirst.Enabled = false;
                    btnAllAthleticYrPrev.Enabled = false;
                    btnAllAthleticYrNext.Enabled = false;
                    btnAllAthleticYrLast.Enabled = false;


                }
                if (currentPageNumber == 1)
                {
                    btnAllAthleticYrFirst.Enabled = false;
                    btnAllAthleticYrPrev.Enabled = false;

                    if (Int32.Parse(lblAllAthleticYrTotalPage.Text) == 1)
                    {
                        allAthleticYrsOfOnePage = allAthleticYrs.GetRange(0, Convert.ToInt32(totalRows));

                        btnAllAthleticYrNext.Enabled = false;
                        btnAllAthleticYrLast.Enabled = false;

                       
                    }
                    else if (Int32.Parse(lblAllAthleticYrTotalPage.Text) == 2)
                    {
                        allAthleticYrsOfOnePage = allAthleticYrs.GetRange(0, PAGE_SIZE * YEAR_PER_ROW);

                        btnAllAthleticYrNext.Enabled = true;
                        btnAllAthleticYrLast.Enabled = true;

                       


                    }


                }


            }
            else
            {
                if (currentPageNumber == 1)
                {
                    allAthleticYrsOfOnePage = allAthleticYrs.GetRange(0, PAGE_SIZE * YEAR_PER_ROW);

                    btnAllAthleticYrFirst.Enabled = false;
                    btnAllAthleticYrPrev.Enabled = false;
                    btnAllAthleticYrNext.Enabled = true;
                    btnAllAthleticYrLast.Enabled = true;

                   
                }



                else
                {
                    btnAllAthleticYrPrev.Enabled = true;
                    btnAllAthleticYrFirst.Enabled = true;

                    if (currentPageNumber == Int32.Parse(lblAllAthleticYrTotalPage.Text))
                    {
                        allAthleticYrsOfOnePage = allAthleticYrs.GetRange((currentPageNumber - 1) * PAGE_SIZE * YEAR_PER_ROW
                            , (Convert.ToInt32(totalRows) - (currentPageNumber - 1) * PAGE_SIZE * YEAR_PER_ROW));

                        btnAllAthleticYrNext.Enabled = false;
                        btnAllAthleticYrLast.Enabled = false;

                    }
                    else
                    {
                        allAthleticYrsOfOnePage = allAthleticYrs.GetRange((currentPageNumber - 1) * PAGE_SIZE * YEAR_PER_ROW, PAGE_SIZE * YEAR_PER_ROW);

                        btnAllAthleticYrNext.Enabled = true;
                        btnAllAthleticYrLast.Enabled = true;

                    }


                }
            }

            // gvAllMeeting.DataSource = allMeetingsOfOnePage;
            // gvAllMeeting.DataBind();
            CreateDynamicTable(allAthleticYrsOfOnePage);


        }

        private int CalculateTotalPages(double totalRows)
        {
            int totalPages = (int)Math.Ceiling(totalRows / (PAGE_SIZE * YEAR_PER_ROW));

            return totalPages;
        }

        protected void ChangePage(object sender, CommandEventArgs e)
        {

            switch (e.CommandName)
            {

                case "First":
                    currentPageNumber = 1;
                    break;

                case "Previous":
                    currentPageNumber = Int32.Parse(lblAllAthleticYrCurrPage.Text) - 1;
                    break;

                case "Next":
                    currentPageNumber = Int32.Parse(lblAllAthleticYrCurrPage.Text) + 1;
                    break;
                case "Last":
                    currentPageNumber = Int32.Parse(lblAllAthleticYrTotalPage.Text);
                    break;
            }


            BindData();
        }

    }
}